package com.marktotop.controller.system;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marktotop.entity.system.ImageAttachment;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.service.IImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Api(tags = "系统模块-文件上传接口")
@RestController
@RequestMapping("/system/upload")
public class UploadController {

    @Autowired
    private IImageService imageService;


    /**
     * 上传图片文件
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件")
    @RequiresPermissions({"upload:image"})
    @RequestMapping(value = "/image",method = RequestMethod.POST)
    public ResultBean uploadImage(MultipartFile file) throws IOException, SystemException {
        String realPath = imageService.uploadImage(file);
        return ResultBean.ok().data("path",realPath).message("上传资源文件成功!");
    }


    /**
     * 附件列表(图片)
     *
     * @return
     */
    @ApiOperation(value = "附件列表", notes = "模糊查询附件列表")
    @RequestMapping(value = "/findImageList",method = RequestMethod.GET)
    public ResultBean findImageList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize,
                                    ImageAttachment image ) {
        return imageService.findImageList(pageNum,pageSize,image);
    }


    /**
     * 删除图片
     * @param id
     * @return
     */
    @ApiOperation(value = "删除图片", notes = "删除数据库记录,删除图片服务器上的图片")
    @RequiresPermissions("attachment:delete")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean delete(@PathVariable Long id) throws SystemException {
        imageService.deleteImage(id);
        return ResultBean.ok().message("删除图片资源成功!");
    }

}
